var validate = {};

$(function() {
    // 菜单点击的都跑到在跳转到 frame 里
    $(".menu_button").attr("target", "frame");
    // iFrame 高度设置
    frameSizeSet();
    $(window).resize(function() {
	frameSizeSet();
    });
    // 时间设置
    timeShow();
    // utils 栏伸缩按钮
    $(".toggle_utils").click(utilToggle);
    $(".toggle_utils").text("<");
    $(".logo").css("margin-top", $(".menu").height());
    
    // 初始化界面显示的数据
    $.post("/QuickLifeServer/GetBusInfosAction", 
	    {"actionflag": "busdetail", "rqinfos": $("#id").val()},
	    function(rtinfos) {
		rt = JSON.parse(rtinfos);
		if (rt.result.toUpperCase() == "SUCCESS") {
		    var busInfos = rt.rtinfos[0];
		    $("#bus_logo").attr("src", busInfos.logo);
		    $("#name").text(busInfos.name);
		} else {
		    $("#fail_info").text(rt.rtinfos);
		    $("#fail_dialog").dialog("open");
		}
    });
    $.post("/QuickLifeServer/BussInfoManageAction",
	    {'actionflag' : 'businfos', 'rqinfos' : $("#id").val()},
	    function(rtinfos) {
		rt = JSON.parse(rtinfos);
		if (rt.result.toUpperCase() == "SUCCESS") {
		    $("#coupons_total").append(rt.rtinfos[0].total);
		    $("#bus_score").append(rt.rtinfos[0].score);
		} else {
		    $("#coupons_total").empty().append("欢迎");
		    $("#bus_score").empty().append("光临");
		}
	    });
    
    // 弹出框初始化
    initDialog();
});

function initDialog() {
    $("#change_password_dialog").dialog({
	autoOpen : false,
	title : "修改密码",
	width : 370,
	buttons : {
	  "修改" : changePassword,
	  "取消" : closeThisDialog
	},
	close : function() {
	    $("#oldPassword, #newPassword, #rePassword").val(null);
	    $("#oldPassword_validate, #password_validate").empty();
	},
	show : {
	    effect : "fade",
	    duration : 200
	},
	hide : {
	    effect : "fade",
	    duration : 300
	}
    });
}

function openPasswordDialog() {
    $("#change_password_dialog").dialog("open");
}

function changePassword() {
    if (validate.password) {
	if ($("#newPassword").val().trim().length > 5){
	    if ($("#rePassword").val().trim() == $("#newPassword").val().trim()) {
		var rqinfos = 
			{"userid" : $("#id").val(), "password" : hex_md5($("#newPassword").val()).toUpperCase()};
		$.post("/QuickLifeServer/BussInfoManageAction", 
			{"actionflag" : "mdfpsw", "rqinfos" : obj2JSON(rqinfos)},
			function(result) {
			    var rt = JSON.parse(result);
			    if (rt.result.toUpperCase() == "SUCCESS") {
				$.post("/QuickLifeServer/WebLogoutAction");
				location.href = "/QuickLifeServer/login.jsp";
			    } else {
				$("#password_validate").html("<span style='color: red;'>密码修改失败</span>");
			    }
			}
		);
	    } else {
		$("#password_validate").html("<span style='color: red;'>新密码不一致</span>");
	    }
	} else {
	    $("#password_validate").html("<span style='color: red;'>密码长度不足</span>");
	}
    } else {
	$("#oldPassword_validate").html("<span style='color: red;'>密码错误</span>");
	validEffect($("#oldPassword"));
    }
}

function checkOldPassword() {
    $("#oldPassword_validate").html("<span style='color: green;'>密码检查中</span>");
    if ($("#oldPassword").val()) {
	var rqinfos = {"userid" : $("#id").val(), "password" : hex_md5($("#oldPassword").val()).toUpperCase()};
	$.post("/QuickLifeServer/WebCheckAction", 
		{"actionflag" : "ckpsw", "rqinfos" : obj2JSON(rqinfos)},
		function( result ) {
		    if (JSON.parse(result).result.toUpperCase() == "SUCCESS") {
			validate.password = true;
			$("#oldPassword_validate").html("<span style='color: green;'>密码正确</span>");
		    } else {
			invalidEffect($("#oldPassword"));
			validate.password = false;
			$("#oldPassword_validate").html("<span style='color: red;'>密码错误</span>");
		    }
		});
    	} else {
    	    	$("#oldPassword_validate").html("<span style='color: red;'>请输入旧密码</span>");
        	invalidEffect($("#oldPassword"));
        	validate.password = false;
    	}
    }

var utilsClose = false;
function utilToggle() {
    if (utilsClose) {
	$(".menu").animate({
	    "left" : "+=170px"
	}, 300);
	$("#frame").animate({
	    "width" : "-=170px",
	    "left" : "+=170px"
	}, 300);
	$(".utils_content").animate({
	    "left" : "+=170px"
	}, 300, function() {
	    $(".toggle_utils").text("<");
	});
    } else {
	$(".menu").animate({
	    "left" : "-=170px"
	}, 300);
	$("#frame").animate({
	    "width" : "+=170px",
	    "left" : "-=170px"
	}, 300);
	$(".utils_content").animate({
	    "left" : "-=170px"
	}, 300, function() {
	    $(".toggle_utils").text(">");
	});
    }
    utilsClose = !utilsClose;
}

function frameSizeSet() {
    // frame框的大小也根据 utils_content而定
    var height = $(".utils_content").outerHeight();
    height = height > $(window).innerHeight() ? height : $(window)
	    .innerHeight();
    $("#frame").height(height - $(".menu_content").height() - 20);
    $("#frame").width(window.innerWidth - $(".utils_content").width() - 20);
    /*
     * // utils_content 高度确定 if ( $(".utils_content").height() <
     * $(window).innerHeight() ) { $(".utils_content").height(window.innerHeight -
     * $(".menu_content").height()); } else {
     * $(".utils_content").height("100%"); }
     */
    // $(".utils_content").height(window.innerHeight);
    $(".utils_content").css("min-height", window.innerHeight);

    // 伸缩按钮确定
    $(".toggle_utils").css("top", $(window).innerHeight() / 5 * 2);
}

function timeShow() {
    var date = new Date();
    var weekText = [ "日", "一", "二", "三", "四", "五", "六" ];
    var tWeek = "星期" + weekText[date.getDay()];
    $("#date_time").html(
	    date.getFullYear() + "/" + addZero(date.getMonth()) + "/"
		    + addZero(date.getDate()) + "&nbsp;" + tWeek + "<br/>"
		    + addZero(date.getHours()) + ":"
		    + addZero(date.getMinutes()) + ":"
		    + addZero(date.getSeconds()));
    setTimeout('timeShow()', 1000);
}